#include "LuaDelegate.h"
#include "CCLuaEngine.h"
#include "tolua_fix.h"
USING_NS_CC;

int LuaDelegate::addScriptHandler(int handler)
{
	for (unsigned i = 0; i < mHandlers.size(); ++i)
	{
		if (*mHandlers[i] == handler)
			return handler;
	}

	mHandlers.push_back(std::shared_ptr<LuaHandler>(new LuaHandler(handler)));

	return handler;
}

void LuaDelegate::removeScriptHandler(int handler)
{
	for (unsigned i = 0; i < mHandlers.size(); ++i)
	{
		if (*mHandlers[i] == handler)
		{
			mHandlers[i] = 0;
		}
	}
}

void LuaDelegate::clearScriptHandler()
{
	for (unsigned i = 0; i < mHandlers.size(); ++i)
	{
		if (mHandlers[i] != 0)
		{
			mHandlers[i] = 0;
		}
	}

	mHandlers.clear();
}

void LuaDelegate::merge_handlers()
{
	LuaHandlerVector::iterator iter = mHandlers.begin();
	while (iter != mHandlers.end())
	{
		if (0 == (*iter))
		{
			iter = mHandlers.erase(iter);
		}
		else
		{
			++iter;
		}
	}
}